<?php

namespace App\Admin\Controllers\Act;

use App\Http\Controllers\Controller;
use App\Model\WishTree;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;

class WishTreeLogController extends Controller
{
    use ModelForm;

    public function index()
    {
        $content = new Content();
        $content->header('许愿树抽奖记录');
        $content->body($this->grid());
        return $content;
    }

    public function edit($id)
    {
        $content = new Content();
        $content->header('编辑');
        $content->body($this->form()->edit($id));
        return $content;
    }

    public function create()
    {
        $content = new Content();
        $content->header('创建');
        $content->body($this->form());
        return $content;
    }

    protected function grid()
    {
        $grid = new Grid(new WishTree());
        $grid->model()->orderBy('id', 'desc');
        $grid->userProfile()->user_profile_id('许愿用户ID');
        $grid->userProfile()->nick_name('昵称');
        $grid->userProfile()->gender('性别')->display(function ($gender) {
            return $gender == 1 ? '男' : ($gender == 0 ? '女' : '-');
        });
        $grid->giftInfo()->id('中奖礼物ID');
        $grid->giftInfo()->name('礼物名');
        $grid->coin('总金币价值')->sortable();
        $grid->num('数量')->sortable();
        $grid->created_at('中奖时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
        });
        return $grid;
    }

    // protected function form()
    // {
    //     $form = new Form(new WishTreeGift());
    //     $form->column(1 / 2, function ($form) {
    //         $form->text('gift_percent', '概率')->required();
    //         $form->text('gift_num', '礼物数量')->default(0)->required();
    //     });
    //     $form->column(1 / 2, function ($form) {
    //         $form->select('gift_id', '礼物')->options(Gift::getList([3, 4]));
    //         $form->select('flag', '是否有效')->options([1 => '有效', 0 => '无效'])->default(1)->required();
    //     });
    //     $form->saving(function (Form $form) {
    //         if (floatval($form->gift_percent) >= 1) return InfoUtil::adminErr('概率设置错误');
    //     });
    //     return $form;
    // }
}
